<?php
defined('IN_LK') or die('Access Denied');
lk::lo_app('admin_common', 'admin', 0);
class admin_manage extends admin_common
{
	/**
	 * model 句柄
	 */
	private $admin_db;
	private $admin_panel_db;
	
	/**
	 * admin_manage句柄
	 */
	private $admin_op;
	
	/**
	 * roles角色
	 */
	private $role;
	
	public function __construct()
	{
		parent::__construct();
		lk::lf_app('admin');
		lk::lo('form', '', 0);
		$this->admin_db = lk::load_model('admin');
		$this->admin_panel_db = lk::load_model('admin_panel');
		$this->admin_op = lk::lo_app('admin_op', 'admin', 1);
		$this->role  = get_cache('admin_role');
	}
	
	/**
	 * 默认方法
	 */
	public function initialize()
	{
		$ac = isset($_GET['ac']) ? (int)$_GET['ac'] : 1;
		if($ac == 1)
		{
			if(!isset($_GET['page']))
			{
				$_GET['page'] = 1;
			}
			$admin_list = $this->admin_db->listinfo('*', '', $_GET['page'], 25, '`userid`');
			$page_nav   = $this->admin_db->page_nav;
			foreach($admin_list as $key => $admin)
			{
				$admin_list[$key]['rolename'] = $this->role[$admin['roleid']];
				$admin_list[$key]['lastlogintime'] = date('Y-m-d H:i:s', $admin['lastlogintime']);
			}
		}
		else
		{
			if(isset($_POST['dosubmit']))
			{
				$info = check_userinfo($_POST['info']);
				if($info['roleid'] == 1)
				{
					showmsg(LANG('role_illegal'), false);
				}
				if(empty($info['password']) || empty($info['password2']) || $info['password'] != $info['password2'] || !is_password($info['password2']))
				{
					showmsg(LANG('password_illegal'), false);
				}
				$admin_fields = array('username', 'password', 'email', 'roleid');
				foreach($info as $k => $v)
				{
					if(!in_array($k, $admin_fields))
					{
						unset($info[$k]);
					}
				}
				//检查用户名和email重复
				if(!is_username($info['username']) || $this->admin_op->is_username_exists($info['username']) ||!is_email($info['email']) || $this->admin_op->is_email_exists($info['email']))
				{
					showmsg(LANG('username_email_exists'), false);
				}
				$info['password'] = password($info['password']);
				if($this->admin_db->insert($info))
				{
					showmsg(LANG('update_success'), true, '?m=admin&c=admin_manage');
				}
				showmsg(LANG('update_false'), false);
			}
			$s = form::select($this->role, 'name="info[roleid]" size="1"', '', '', array(1=>'disabled="disabled"'));
		}
		include self::admin_t('admin_manage');
	}
	
	/**
	 * 编辑管理员
	 */
	public function admin_edit()
	{
		if(isset($_POST['dosubmit']))
		{
			$info = check_userinfo($_POST['info']);
			$userid = (int)$info['userid'];
			if(!empty($info['pwd']) && !empty($info['pwd2']) && $info['pwd'] == $info['pwd2'])
			{
				$this->admin_op->edit_pwd($info['userid'], $info['pwd2']);
			}
			if($info['roleid'] == 1 && $info['userid'] != 1)
			{
				showmsg(LANG('role_illegal'), false);
			}
			$admin_fields = array('email', 'roleid');
			foreach($info as $k => $v)
			{
				if(!in_array($k, $admin_fields))
				{
					unset($info[$k]);
				}
			}
			$this->admin_db->update($info, "`userid`='$userid'");
			showmsg(LANG('update_success'), '', '', '', 'edit');
		}
		else
		{
			$userid = isset($_GET['userid']) ? $_GET['userid'] : 0;
			$admin  = $this->admin_db->get_one('username,roleid,email', "`userid`='$userid'");
			if(!$admin)
			{
				showmsg(LANG('parameters_error'), false);
			}
			extract($admin, EXTR_OVERWRITE);
			$roles  = $this->role;
			$self   = $userid == $_SESSION['userid'] ? ' disabled="disabled"' : '';
			$select = form::select($roles, 'name="info[roleid]" size="1"' . $self, $roleid, '', array(1=>'disabled="disabled"'));
			include self::admin_t('admin_manage_edit');
		}
	}
	
	/**
	 * 删除管理员
	 */
	public function admin_del()
	{
		$userid = (int)$_GET['userid'];
		if($userid == 1 || $userid == $_SESSION['userid'])
		{
			exit('0');
		}
		$this->admin_db->delete("`userid`='$userid'");
		$this->admin_panel_db->delete("`userid`='$userid'");
		exit('1');
	}
	
	/**
	 * 检查Email是否重复 不检查自己的
	 */
	public function check_email_ajax()
	{
		$res = false;
		$userid = isset($_GET['userid']) ? (int)$_GET['userid'] : 0;
		if(!$userid)
		{
			exit('0');
		}
		if(isset($_GET['email']) && $_GET['email'])
		{
			$email = $_GET['email'];
			$res = $this->admin_db->get_one('userid', "`email`='$email' AND `userid`!='$userid'");
		}
		(boolean)$res ? exit('0') : exit('1');
	}
	
	/**
	 * 添加时email是否重复 重复返回0
	 */
	public function check_eamil_add()
	{
		if(!is_email($_GET['email']) || $this->admin_op->is_email_exists($_GET['email']))
		{
			exit('0');
		}
		exit('1');
	}
	
	/**
	 * 检查用户名是否重复 重复返回0
	 */
	public function check_username_ajax()
	{
		if(!is_username($_GET['username']) || $this->admin_op->is_username_exists($_GET['username']))
		{
			exit('0');
		}
		exit('1');
	}
}